package com.stylefeng.guns.modular.owBackstageMgr.newsThemeMgr.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.ErrorTip;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.warpper.NewsThemeWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.NewsTheme;
import com.stylefeng.guns.modular.owBackstageMgr.newsThemeMgr.service.INewsThemeService;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 新闻主题控制器
 *
 * @author fengshuonan
 * @Date 2018-06-24 10:39:40
 */
@Controller
@RequestMapping("/newsTheme")
public class NewsThemeController extends BaseController {

    private String PREFIX = "/newsThemeMgr/newsTheme/";

    @Autowired
    private INewsThemeService newsThemeService;

    /**
     * 跳转到新闻主题首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "newsTheme.html";
    }

    /**
     * 跳转到添加新闻主题
     */
    @RequestMapping("/newsTheme_add")
    public String newsThemeAdd() {
        return PREFIX + "newsTheme_add.html";
    }

    /**
     * 跳转到修改新闻主题
     */
    @RequestMapping("/newsTheme_update/{newsThemeId}")
    public String newsThemeUpdate(@PathVariable Integer newsThemeId, Model model) {
        NewsTheme newsTheme = newsThemeService.selectById(newsThemeId);
        model.addAttribute("item",newsTheme);
        LogObjectHolder.me().set(newsTheme);
        return PREFIX + "newsTheme_edit.html";
    }

    /**
     * 获取新闻主题列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String name) {
        Page<NewsTheme> page = new PageFactory<NewsTheme>().defaultPage();
        List<Map<String, Object>> result = newsThemeService.getNewsThemes(page, name,  page.getOrderByField(), page.isAsc());
        page.setRecords((List<NewsTheme>) new NewsThemeWarpper(result).warp());
        return super.packForBT(page);
    }

    /**
     * 新增新闻主题
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(NewsTheme newsTheme) {
        if (ToolUtil.isOneEmpty(newsTheme, newsTheme.getName())) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        newsTheme.setCreateTime(new Date());
        newsTheme.setUpdateTime(new Date());
        newsTheme.insert();
        return SUCCESS_TIP;
    }

    /**
     * 删除新闻主题
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer newsThemeId) {
        NewsTheme newsTheme = newsThemeService.selectById(newsThemeId);
        if (ToolUtil.isNotEmpty(newsTheme)) {
            newsTheme.setIsDel(1);
            newsTheme.setUpdateTime(new Date());
            newsThemeService.updateById(newsTheme);
            return SUCCESS_TIP;
        }
        return new ErrorTip(-1,"找不到要删除的记录~");
    }

    /**
     * 修改新闻主题
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(NewsTheme newsTheme) {
        if (ToolUtil.isOneEmpty(newsTheme, newsTheme.getId(), newsTheme.getName())) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        NewsTheme old = this.newsThemeService.selectById(newsTheme.getId());
        old.setName(newsTheme.getName());
        old.updateById();
        return SUCCESS_TIP;
    }

    /**
     * 新闻主题详情
     */
    @RequestMapping(value = "/detail/{newsThemeId}")
    @ResponseBody
    public Object detail(@PathVariable("newsThemeId") Integer newsThemeId) {
        return newsThemeService.selectById(newsThemeId);
    }

    /**
     * 前台ajax获取全部的主题，id做value，name做下拉选项
     */
    @RequestMapping(value="/getAllThemes")
    @ResponseBody
    public String getAllThemes(){
        EntityWrapper<NewsTheme> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("is_del",0);
        List<NewsTheme> newsThemes = newsThemeService.selectList(entityWrapper);
        return JSON.toJSONString(newsThemes);
    }
}
